package com.smzd.ccms.actions;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.ccms.business.TemplateService;
import com.smzd.ccms.core.AbstractSiteResourceController;
import com.smzd.ccms.domain.Site;
import com.smzd.ccms.domain.Template;
import com.smzd.framework.annotation.Access;
import com.smzd.framework.core.business.IFGService;
import com.smzd.framework.system.domain.Meeting;
import com.smzd.framework.utils.db.ORPage;

@Controller
@RequestMapping("/a/{site}/templates")
@Access("TAG_ROLE_CCMS")
public class TemplateController extends AbstractSiteResourceController<Template> {
	@Autowired
	private TemplateService service;

	@Override
	protected IFGService<Template> getService() {
		return service;
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	@Override
	public String toAddPage(Map<String, Object> model, @PathVariable("site") String siteStr, Template elem,
			BindingResult result) throws Exception {
		return super.toAddPage(model, siteStr, elem, result);
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public String postAddData(Map<String, Object> model, @PathVariable("site") String siteStr, Template elem,
			BindingResult result) throws Exception {
		return super.postAddData(model, siteStr, elem, result);
	}

	@RequestMapping(value = { "/index.html", "" }, method = RequestMethod.GET)
	@Override
	public String toListPage(Map<String, Object> model, @PathVariable("site") String siteStr, Template elem,
			BindingResult result) throws Exception {
		return super.toListPage(model, siteStr, elem, result);
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public String getListData(Map<String, Object> model, @PathVariable("site") String siteStr,
			ORPage<Meeting, Template> page, BindingResult result) throws Exception {
		return super.getListData(model, siteStr, page, result);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	@Override
	public String toEditPage(Map<String, Object> model, @PathVariable("site") String siteStr, Template elem,
			BindingResult result) throws Exception {
		Site site = setCurrentSite(model, siteStr);
		elem = service.getItemById(elem.getId());
		if (elem != null) {// XXX 这是要干嘛？检查是不是这个站点的栏目是对的，如果是，正常返回，如果不是呢？
			if (site.getOwner().getId() - elem.getOwner().getId() == 0) {
				model.put("_site", site);
				return super.toEditPage(model, elem, result);
			}
		}
		throw new Exception("当前站点下没有这个模板？");
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public String postEditData(Map<String, Object> model, @PathVariable("site") String siteStr, Template elem,
			BindingResult result) throws Exception {
		return super.postEditData(model, siteStr, elem, result);
	}

	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public String postRemoveData(Map<String, Object> model, @PathVariable("site") String siteStr, Template elem,
			BindingResult result) throws Exception {
		return super.postRemoveData(model, siteStr, elem, result);
	}
}